import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _isFunction from "lodash/isFunction";
import _map from "lodash/map";

var _class, _class2;

/**
 * DynamicPage.js
 * @date 2018/10/22
 */
import React from 'react';
import { withRouter } from 'dva/router';
import { getResponse, getCurrentOrganizationId } from "../../utils/utils";
import request from "../../utils/request"; // import { queryConfigsByPageCode } from 'cpaas-front-cpfm/lib/services/uiPageService';

import DynamicComponent from "./index";

function queryConfigsByPageCode(organizationId, pageCode) {
  return request("cpfm/v1/".concat(organizationId, "/ui-pages/common/").concat(pageCode), {
    method: 'GET'
  });
}

var DynamicPage = withRouter(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(DynamicPage, _React$Component);

  var _super = _createSuper(DynamicPage);

  // 存储 DynamicComponent 对应组件的ref;
  function DynamicPage(props) {
    var _this;

    _classCallCheck(this, DynamicPage);

    _this = _super.call(this, props);
    _this.request = request;
    _this.getResponse = getResponse;
    _this.ref = {};
    _this.state = {
      isStaticConfig: false,
      templates: [] // 模板

    };
    _this.onRef = _this.onRef.bind(_assertThisInitialized(_this));
    var pageConfig = props.pageConfig,
        script = props.script;
    var initTemplates = pageConfig && pageConfig.fields || [];
    _this.state = {
      isStaticConfig: !!pageConfig,
      templates: initTemplates
    };

    if (script) {
      // eslint-disable-next-line
      eval(script)(_assertThisInitialized(_this));
    }

    return _this;
  }

  _createClass(DynamicPage, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var params = this.props.params;
      var isStaticConfig = this.state.isStaticConfig;
      var templates = this.state.templates;

      if (isStaticConfig) {
        templates = this.props.pageConfig && this.props.pageConfig.fields || [];
      }

      return /*#__PURE__*/React.createElement(React.Fragment, null, _map(templates, function (template) {
        return /*#__PURE__*/React.createElement(DynamicComponent, {
          params: params,
          template: template,
          onRef: _this2.onRef(template),
          key: template.templateCode,
          context: _this2
        });
      }));
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      var isStaticConfig = this.state.isStaticConfig;
      var onRef = this.props.onRef;

      if (!isStaticConfig) {
        this.fetchPageConfig();
      }

      if (onRef && _isFunction(onRef)) {
        onRef(this);
      }
    }
    /**
     * @param {Object} template - 模板的配置
     * @returns {Function} 将 DynamicComponent 对应组件的this设置到 this.ref 中
     */

  }, {
    key: "onRef",
    value: function onRef(template) {
      var _this3 = this;

      return function (tplRef) {
        _this3.ref[template.templateCode] = tplRef;
      };
    }
    /**
     * 获取 page 对应的 配置
     */

  }, {
    key: "fetchPageConfig",
    value: function fetchPageConfig() {
      var _this4 = this;

      var _this$props = this.props,
          pageCode = _this$props.pageCode,
          _this$props$organizat = _this$props.organizationId,
          organizationId = _this$props$organizat === void 0 ? getCurrentOrganizationId() : _this$props$organizat;

      if (pageCode) {
        queryConfigsByPageCode(organizationId, pageCode).then(function (res) {
          var page = getResponse(res) || {}; // todo 在这里需要设置 script

          if (page.script) {
            // eslint-disable-next-line
            eval(page.script)(_this4);
          }

          _this4.setState({
            templates: page.fields || []
          });
        });
      }
    }
  }]);

  return DynamicPage;
}(React.Component), _class2.defaultProps = {
  pageCode: undefined,
  pageConfig: undefined,
  organizationId: undefined
}, _class2)) || _class;

export { DynamicPage as default };